package com.x.erp.service;

import com.alibaba.fastjson.JSON;
import com.x.erp.dao.CarDao;
import com.x.erp.dao.CarImageDao;
import com.x.erp.dao.ModelMappingXyDao;
import com.x.erp.dao.UserDao;
import com.x.erp.model.Car;
import com.x.erp.model.CarImage;
import com.x.erp.model.User;
import com.x.erp.thirdparty.CarMappingXy;
import com.x.erp.thirdparty.ModelMappingXy;
import com.x.erp.thirdparty.UsedCarPictureModel;
import com.x.erp.thirdparty.XyCarInfo;
import com.x.erp.util.Constant;
import com.x.erp.util.XyApi;
import com.x.erp.util.XyResult;
import com.x.framework.service.BaseService;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class XyService extends BaseService {

	private Logger logger = LoggerFactory.getLogger(XyService.class);
	
	@Autowired
	private ModelMappingXyDao modelMappingXyDao;

	@Autowired
	private CarMappingXyService carMappingXyService;

	@Autowired
	private CarImageDao carImageDao;

	@Autowired
	private XyApi xyApi;

	@Autowired
	private UserDao userDao;
	
	@Autowired
	private CarDao carDao;
	
	// 发布车源
	public void insertCarXy(Car car) {
		if(car != null && StringUtils.isNotEmpty(car.getCarId())) {
			//获取车辆信息
			car =  getCarInfo(car);
			if(car == null){
				logger.info("获取车辆信息失败！");
				return;
			}
			// 判断是否为第一次发布车源
			CarMappingXy carMappingXy = new CarMappingXy();
			carMappingXy.setCarId(car.getCarId());
			List<CarMappingXy> selectXyCarList = carMappingXyService.selectXyCarList(carMappingXy);
			if(selectXyCarList != null && selectXyCarList.size() > 0){
				try {
					XyResult carOnSale = xyApi.setCarOnSale(selectXyCarList.get(0).getCarId(), true);
					if (carOnSale == null){
						logger.info("行圆重新上架返回数据有误！");
						return;
					}
					if(!carOnSale.getCode().equals("0000000")){
						logger.info("行圆重新上架车源失败，提示码：" + carOnSale.getCode() + "  ，提示信息：" + new String(carOnSale.getMessage().getBytes(),"UTF-8"));
						return;
					}
				} catch (Exception e) {
					logger.error("行圆重新发布车源失败！" , e);
					return;
				}
			}else {
				//获取行圆车源信息
				XyCarInfo xyCarInfo = getXyCarInfo(car);
				if(xyCarInfo == null){
					logger.info("获取行圆车辆信息失败！");
					return;
				}
				// 发布车源
				Integer result = null;
				try {
					logger.info(JSON.toJSONString(xyCarInfo));
					XyResult insertCar = xyApi.insertCar(xyCarInfo);
					if (insertCar == null){
						logger.info("行圆发布车源返回数据有误！");
						return;
					}
					if (!insertCar.getCode().equals("0000000")) {
						logger.info("行圆发布车源失败，提示码：" + insertCar.getCode() + "  ，提示信息：" + new String(insertCar.getMessage().getBytes(),"UTF-8"));
						return;
					}
					result = insertCar.getData();
				} catch (Exception e) {
					logger.error("行圆发布车辆失败！", e);
					return;
				}
				//将返回的车辆id存起来
				carMappingXy.setMappingCarId(result);
				CarMappingXy carMappingXyCar = carMappingXyService.insertXyCar(carMappingXy);
				if (carMappingXyCar != null){
					logger.info("行圆发布车辆成功！行圆车辆ID ：" + carMappingXyCar.getMappingCarId());
				}
			}
		}
	}

	// 修改车源
	public void updateCarXy(Car car) {
		if(car != null && StringUtils.isNotEmpty(car.getCarId())) {
			CarMappingXy carMappingXy = new CarMappingXy();
			carMappingXy.setCarId(car.getCarId());
			List<CarMappingXy> selectXyCarList = carMappingXyService.selectXyCarList(carMappingXy);
			if (selectXyCarList == null || selectXyCarList.size()==0){
				logger.info("行圆无此车辆！");
				return;
			}
			//获取车辆信息
			car =  getCarInfo(car);
			if(car == null){
				logger.info("获取车辆信息失败！");
				return;
			}
			//获取行圆车源信息
			XyCarInfo xyCarInfo = getXyCarInfo(car);
			if(xyCarInfo == null){
				logger.info("获取行圆车辆信息失败！");
				return;
			}
			// 修改车源
			try {
				logger.info(JSON.toJSONString(xyCarInfo));
				XyResult insertCar = xyApi.insertCar(xyCarInfo);
				if (insertCar == null){
					logger.info("行圆修改车源返回数据有误！");
					return;
				}
				if (!insertCar.getCode().equals("0000000")) {
					logger.info("行圆修改车源失败，提示码：" + insertCar.getCode() + "  ，提示信息：" + new String(insertCar.getMessage().getBytes(),"UTF-8"));
					return;
				}
				logger.info("行圆修改车辆成功！");
			} catch (Exception e) {
				logger.error("行圆修改车辆失败！", e);
				return;
			}
		}
	}

	// 删除车源
	public void deleteCarXy(Car car) {
		if(car != null && StringUtils.isNotEmpty(car.getCarId())){
			try {
				CarMappingXy carMappingXy = new CarMappingXy();
				carMappingXy.setCarId(car.getCarId());
				List<CarMappingXy> selectXyCarList = carMappingXyService.selectXyCarList(carMappingXy);
				if(selectXyCarList != null && selectXyCarList.size() > 0){
					XyResult deleteCar = xyApi.deleteCar(selectXyCarList.get(0).getCarId(), true);
					if (deleteCar == null){
						logger.info("行圆删除数据有误！");
						return;
					}
					if(!deleteCar.getCode().equals("0000000")){
						logger.info("行圆车源删除失败，提示码：" + deleteCar.getCode() + "  ，提示信息：" + new String(deleteCar.getMessage().getBytes(),"UTF-8"));
						return;
					}
					logger.info("行圆删除车源成功！");
				}
			} catch (Exception e) {
				logger.error("行圆删除车源失败！", e);
				return;
			}
		}
	}

	//设置车源下架
	public void updateCarSaleState30(Car car) {
		if(car != null && StringUtils.isNotEmpty(car.getCarId())){
			try {
				CarMappingXy carMappingXy = new CarMappingXy();
				carMappingXy.setCarId(car.getCarId());
				List<CarMappingXy> selectXyCarList = carMappingXyService.selectXyCarList(carMappingXy);
				if(selectXyCarList != null && selectXyCarList.size() > 0){
					XyResult carOnSale = xyApi.setCarOnSale(selectXyCarList.get(0).getCarId(), false);
					if (carOnSale == null){
						logger.info("行圆设置车源下架返回数据有误！");
						return;
					}
					if(!carOnSale.getCode().equals("0000000")){
						logger.info("行圆车源下架失败，提示码：" + carOnSale.getCode() + "  ，提示信息：" + new String(carOnSale.getMessage().getBytes(),"UTF-8"));
						return;
					}
					logger.info("行圆设置车源下架成功！");
				}
			} catch (Exception e) {
				logger.error("行圆车源下架失败！", e);
				return;
			}
		}
	}

	// 车源出库(设为已售)
	public void updateCarStockState40(Car car) {
		if(car != null && StringUtils.isNotEmpty(car.getCarId())) {
			try {
				CarMappingXy carMappingXy = new CarMappingXy();
				carMappingXy.setCarId(car.getCarId());
				List<CarMappingXy> selectXyCarList = carMappingXyService.selectXyCarList(carMappingXy);
				if(selectXyCarList != null && selectXyCarList.size() > 0){
					XyResult carSold = xyApi.setCarSold(selectXyCarList.get(0).getCarId(), false);
					if (carSold == null){
						logger.info("行圆车源出库返回数据有误！");
						return;
					}
					if(!carSold.getCode().equals("0000000")){
						logger.info("行圆车源出库失败，提示码：" + carSold.getCode() + "  ，提示信息：" + new String(carSold.getMessage().getBytes(),"UTF-8"));
						return;
					}
					logger.info("行圆设置车源出库成功！");
				}
			} catch (Exception e) {
				logger.error("行圆车源出库失败！", e);
				return;
			}
		}
	}

	//获取行圆车辆信息方法
	private XyCarInfo getXyCarInfo(Car car){
		//获取图片信息
		CarImage carImage = new CarImage();
		UsedCarPictureModel usedCarPictureModel = new UsedCarPictureModel();
		carImage.setCarId(car.getCarId());
		try {
			List<CarImage> carImages = carImageDao.selectCarImageList(carImage, null);
			if(carImages != null && carImages.size() > 0){
				String imageUrl = null;
				for (CarImage carImg : carImages) {
					imageUrl = Constant.FDFS_CONTENT_PATH + carImg.getImageUrl();
					if(carImg.getImageKey().equals("control_booth")){
						usedCarPictureModel.setControl_booth(imageUrl);
					}
					if(carImg.getImageKey().equals("driving_seat")){
						usedCarPictureModel.setDriving_seat(imageUrl);
					}
					if(carImg.getImageKey().equals("right_rear")){
						usedCarPictureModel.setRight_rear(imageUrl);
					}
					if(carImg.getImageKey().equals("odograph")){
						usedCarPictureModel.setOdograph(imageUrl);
					}
					if(carImg.getImageKey().equals("left_anterior")){
						usedCarPictureModel.setLeft_anterior(imageUrl);
					}
					if(carImg.getImageKey().equals("open_trunk")){
						usedCarPictureModel.setOpen_trunk(imageUrl);
					}
					if(carImg.getImageKey().equals("engine_bay")){
						usedCarPictureModel.setEngine_bay(imageUrl);
					}
					if(carImg.getImageKey().equals("right_side")){
						usedCarPictureModel.setRight_side(imageUrl);
					}
					if(carImg.getImageKey().equals("tyre")){
						usedCarPictureModel.setTyre(imageUrl);
					}
				}
			}
		} catch (Exception e) {
			logger.error("获取图片失败！", e);
			return null;
		}

		// 获取车型库关系
		ModelMappingXy modelMappingXy = new ModelMappingXy();
		modelMappingXy.setModelId(car.getModelId());
		Integer carId = null;
		try {
			List<ModelMappingXy> xyModelList = modelMappingXyDao.selectXyModelList(modelMappingXy);
			if (xyModelList == null || xyModelList.size() == 0){
				logger.info("获取车型库关系失败！");
				return null;
			}
			if (xyModelList.get(0).getXyModelId() == 0){
				logger.info("行圆车型库无此车型，发布失败！");
				return null;
			}
			carId = xyModelList.get(0).getXyModelId();
		} catch (Exception e) {
			logger.error("获取车型库关系失败！", e);
			return null;
		}

		//获取销售人员信息
		User user = new User();
		user.setUserId(car.getSaleUserId());
		String salesName = null;
		String salesTel = null;
		try {
			List<User> userList = userDao.selectUserList(user);
			if (userList == null || userList.size() == 0){
				logger.info("获取销售人员失败！");
				return null;
			}
			salesName = userList.get(0).getUserName();
			salesTel = userList.get(0).getUserMobile();
		} catch (Exception e) {
			logger.error("获取销售人员失败！", e);
			return null;
		}

		XyCarInfo xyCarInfo = new XyCarInfo(car, usedCarPictureModel, carId, salesName, salesTel);
		return xyCarInfo;
	}

	//获取车辆信息方法
	private Car getCarInfo(Car car){
		try {
			Car selectCar = new Car();
			selectCar.setCarId(car.getCarId());
			car = carDao.selectCar(selectCar);
			if(car == null){
				logger.info("获取车辆信息失败！");
				return null;
			}
		} catch (Exception e) {
			logger.error("获取车辆信息失败！", e);
			return null;
		}
		return car;
	}

}
